﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.Web.Common;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.FoodTesting.VcFood
{
    public partial class VcAssayEdit : System.Web.UI.Page
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_VcData tVcData = new T_VcData();              //Vc检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["FoodTime"] != null && Request.QueryString["FoodNum"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        public void LoadData()
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());
            
            E_VcData md_VcData=new E_VcData();
            md_VcData.VcTime = date;
            md_VcData.RawId = RawId;
            List<E_VcData> eVcDataList = tVcData.GetVcDataListByVcTime(md_VcData);

            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 1;//Vc检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eFoodRecord != null)
            {
                this.labMean.Text = eFoodRecord.FoodResult;
            }

            bool IsShowDefault = false; //是否显示默认值
            if (eVcDataList != null)
            {
                int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
                E_VcData eVcData = eVcDataList.Where(p => p.FoodNum == FoodNum).FirstOrDefault();//获取第一次检测记录
                if (eVcData != null)
                {
                    this.txt_C.Text = eVcData.Vc_C;
                    this.txt_V.Text = eVcData.Vc_V;
                    this.txt_V1.Text = eVcData.Vc_V1;
                    this.txt_V2.Text = eVcData.Vc_V2;

                    //this.txt_2.Text = eVcData.Vc_2;
                    this.txt_V_2.Text = eVcData.Vc_2V;
                    this.txt_A.Text = eVcData.Vc_A;
                    this.txt_W.Text = eVcData.Vc_W;

                    this.txt_T.Text = eVcData.Vc_T;
                }
                else
                {
                    IsShowDefault = true;
                }
            }
            else
            {
                IsShowDefault = true; 
            }

            if(IsShowDefault) //若没有则显示最后一次设置的默认值
            {
                E_VcData md = new E_VcData();
                E_VcData eVcData = tVcData.GetModelByTimeDesc(md);
                if (eVcData != null)
                {
                    this.txt_C.Text = eVcData.Vc_C;
                    this.txt_V.Text = eVcData.Vc_V;
                    this.txt_V1.Text = eVcData.Vc_V1;
                    this.txt_V2.Text = eVcData.Vc_V2;
                    
                    this.lab_DefaultTime.Text = Convert.ToDateTime(eVcData.VcTime).ToString();
                }
                
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawId = Convert.ToInt32(Request.QueryString["RawID"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"]);
            int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
            try
            {
                double VC_C = Convert.ToDouble(this.txt_C.Text.Trim());
                double VC_V = Convert.ToDouble(this.txt_V.Text.Trim());
                double VC_V1 = Convert.ToDouble(this.txt_V1.Text.Trim());
                double VC_V2 = Convert.ToDouble(this.txt_V2.Text.Trim());
                double VC_T = (VC_C * VC_V) / (VC_V1 - VC_V2);
                Convert.ToInt32(VC_T);

                double VC_2 = 2;//Convert.ToDouble(this.txt_2.Text.Trim());  这是一个死值
                double VC_V_2 = Convert.ToDouble(this.txt_V_2.Text.Trim());
                double VC_A = Convert.ToDouble(this.txt_A.Text.Trim());
                double VC_W = Convert.ToDouble(this.txt_W.Text.Trim());
                double VC_Result = ((VC_2 - VC_V_2) * VC_T * VC_A / VC_W) * 100;
                Convert.ToInt32(VC_W);

                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);

                E_VcData eVcData = new E_VcData();
                eVcData.RawId = RawId;
                eVcData.RawName = eRawMaterial.RAWName;
                eVcData.Vc_C = VC_C.ToString();
                eVcData.Vc_V = VC_V.ToString();
                eVcData.Vc_V1 = VC_V1.ToString();
                eVcData.Vc_V2 = VC_V2.ToString();
                eVcData.Vc_T = VC_T.ToString();
                eVcData.Vc_2 = VC_2.ToString();
                eVcData.Vc_2V = VC_V_2.ToString();
                eVcData.Vc_A = VC_A.ToString();
                eVcData.Vc_W = VC_W.ToString();
                eVcData.Vc_Result = VC_Result.ToString("f2");
                eVcData.VcTime = date;
                eVcData.FormulaTID = 1;
                eVcData.FormulaVCID = 1;
                eVcData.FoodNum = FoodNum;

                tVcData.RemoveByFoodNum(eVcData);   //删除现有的检测记录
                tVcData.Add(eVcData);               //添加检测记录
                this.txt_T.Text = eVcData.Vc_T;
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('输入有误！');</script>");
            }
        }

        //计算平均值
        protected void btnMean_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_VcData md_VcData = new E_VcData();
            md_VcData.VcTime = date;
            md_VcData.RawId = RawId;
            List<E_VcData> eVcDataList = tVcData.GetVcDataListByVcTime(md_VcData);


            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 1;//Vc检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eVcDataList == null || eVcDataList.Count < 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先提交检测数据！');</script>");
                return;
            }

            double Result = 0;//检测结果
            eVcDataList.ForEach(p=>{
                Result += Convert.ToDouble(p.Vc_Result);
            });

            if (eFoodRecord != null)  //若存在则进行修改
            {
                eFoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                tFoodRecord.Update(eFoodRecord);
                this.labMean.Text = eFoodRecord.FoodResult;
            }
            else
            {
                md_FoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                md_FoodRecord.RawName = eRawMaterial.RAWName;
                tFoodRecord.Add(md_FoodRecord);
                this.labMean.Text = md_FoodRecord.FoodResult;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('计算完毕！');parent.window.RefValue();</script>");
        }
    }
}